﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SistemaInventarioMVC.Models.Repository;
using SistemaInventarioMVC.Models.Entidades;
using SistemaInventarioMVC.Models.Context;

namespace SistemaInventarioMVC.Controllers
{
    public class AlmacenController : Controller
    {
        UnitOfWork unitOfWork = null;
        SistemaInventarioDbContext context = null;

        public AlmacenController() 
        {
            unitOfWork = new UnitOfWork();
        }
        public ActionResult Index()
        {
            return View(unitOfWork.Almacen.GetAll());
        }


        public ActionResult Detalles(int id)
        {
            return View(unitOfWork.Almacen.GetByID(id));
        }

        public ActionResult Crear()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Crear(Almacen almacen)
        {
            try
            {
                unitOfWork.Almacen.Insert(almacen);
                unitOfWork.Save();
                return RedirectToAction("Index");
            }
            catch
            {
                ModelState.AddModelError("", "Error al agregar el registro. Si el problema persiste"+
                    " contacte su administrador de sistemas.");
                return View();
            }
        }


        public ActionResult Editar(int id)
        {
            return View(unitOfWork.Almacen.GetByID(id));
        }

        [HttpPost]
        public ActionResult Editar(int id, Almacen almacen)
        {
            try
            {
                unitOfWork.Almacen.Update(almacen);
                unitOfWork.Save();    
                return RedirectToAction("Index");
            }
            catch
            {
                ModelState.AddModelError("","Error al actualizar el registro. Si el problema persiste contacte su" 
                +"administrador de sistemas.");
                return View();
            }
        }


        public ActionResult Borrar(int id)
        {
            return View(unitOfWork.Almacen.GetByID(id));
        }


        [HttpPost]
        public ActionResult Borrar(int id, Almacen collection)
        {
            try
            {
                unitOfWork.Almacen.Delete(id);
                unitOfWork.Save();
                return RedirectToAction("Index");
            }
            catch
            {
                ModelState.AddModelError("", "Error al eliminar el registro. Si el problema persiste" 
                +"contacte su administrador de sistemas.");
                return View();
            }
        }
    }
}
